import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

# 读取文件
file_path = 'score.txt'
scores = pd.read_csv(file_path, header=None, names=['Score'])

# 检查是否正确读取
print(scores.head())


def main():
    # 生成柱状图
    plt.figure(figsize=(8, 6))
    score_counts = scores['Score'].value_counts().sort_index()

    # 绘制柱状图，修复x轴显示为索引的问题
    plt.bar(score_counts.index, score_counts.values, color='skyblue')

    # 设置x轴刻度为分数值
    plt.xticks(score_counts.index, score_counts.index, rotation=0)

    # 添加标题和标签
    plt.title('Score Distribution', fontsize=16)
    plt.xlabel('Scores', fontsize=12)
    plt.ylabel('Frequency', fontsize=12)
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.show()


def main_compare():
    # 生成柱状图
    plt.figure(figsize=(12, 6))
    score_counts = scores['Score'].value_counts().sort_index()

    # 绘制实际分数分布柱状图
    plt.bar(score_counts.index, score_counts.values, color='skyblue', label='Actual Distribution')

    # 绘制标准正态分布
    mean = scores['Score'].mean()  # 中心点
    std_dev = 5  # 标准差
    x = np.arange(70, 96)
    y = norm.pdf(x, mean, std_dev) * len(scores) * (score_counts.index.max() - score_counts.index.min()) / len(
        score_counts)
    plt.plot(x, y, color='orange', lw=2, label='Normal Distribution')

    # 设置x轴刻度为分数值
    plt.xticks(score_counts.index, score_counts.index, rotation=0)

    # 添加标题和标签
    plt.title('Score Distribution vs Normal Distribution', fontsize=16)
    plt.xlabel('Scores', fontsize=12)
    plt.ylabel('Frequency', fontsize=12)
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.legend()
    plt.show()


if __name__ == '__main__':
    main_compare()
